package com.lemon.smartsafety.ui.fragment.safe.code;

import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ldzs.recyclerlibrary.Mode;
import com.ldzs.recyclerlibrary.PullToRefreshRecyclerView;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.safe.inspection.FireCheckLastDetailVo;
import com.lemon.smartsafety.entity.safe.inspection.FireCheckRecordVo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.InspectionServices;
import com.lemon.smartsafety.listener.PullRefreshListener;
import com.lemon.smartsafety.result.safe.ResultCheckNfc;
import com.lemon.smartsafety.tag.BaseTAG;
import com.lemon.smartsafety.ui.adapter.safe.inspection.CheckItemRecordAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafety.view.list.MultiStateView;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import kale.adapter.LemonRcvAdapter;

/**
 * 项目名称：SmartSafeCode
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/12/21 16:59
 * 修改人：Fargo
 * 修改时间：2016/12/21 16:59
 * 修改备注：
 */
public class CodeCheckItemRecordListFragment extends BaseFragment implements PullRefreshListener {
    private LoggerHelper loggerHelper = new LoggerHelper(CodeCheckItemRecordListFragment.class);


    @Bind(R.id.pullToRefreshRecyclerView)
    public PullToRefreshRecyclerView mPullToRefreshRecyclerView;

    @Bind(R.id.multiStateView)
    public MultiStateView mMultiStateView;


    @Bind(R.id.ll_position)
    LinearLayout llPosition;

    @Bind(R.id.tv_tip)
    TextView tvTip;


    protected int currentPage = Constants.DEFAULT_FIRST_PAGE_NUMBER;

    protected int pageSize = Constants.PAGE_SIZE;

    public List<FireCheckLastDetailVo> mDataList = new ArrayList<>();

    protected LemonRcvAdapter<FireCheckLastDetailVo> mAdapter;

    private ResultCheckNfc resultCheckNfc;



    /**
     * bundle info
     */


    public static CodeCheckItemRecordListFragment newInstance() {
        return new CodeCheckItemRecordListFragment();
    }


    private String mKeyWord = "";

    protected void getBaseArguments() {
        if (getArguments() != null) {
            mKeyWord = StringUtils.nullValue(getArguments().getString(BundleKey.KEY_WORD));
            resultCheckNfc = (ResultCheckNfc) getArguments().getSerializable(BaseTAG.ResultCheckNfc_ItemDATA);
        }

    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_refresh_no_title;
    }

    @Override
    protected void initViews() {
        pageSize = Integer.MAX_VALUE;//这样设置一下，就不就有加载更多了
        LinearLayoutManager layoutManager = new LinearLayoutManager(IApplication.context(), LinearLayoutManager.VERTICAL, false);
        mPullToRefreshRecyclerView.setRefreshMode(Mode.PULL_FROM_START);
        mPullToRefreshRecyclerView.setLayoutManager(layoutManager);
        mAdapter = getLemonAdapter();
        mPullToRefreshRecyclerView.setAdapter(mAdapter);
        mPullToRefreshRecyclerView.setOnPullUpToRefreshListener(new PullToRefreshRecyclerView.OnPullUpToRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = Constants.DEFAULT_FIRST_PAGE_NUMBER;
                onPullRefresh();
            }
        });


        mPullToRefreshRecyclerView.setRefreshMode(Mode.PULL_FROM_START);

        mPullToRefreshRecyclerView.beginRefreshing();

        initGalleryFinalImageLoader();


        llPosition.setVisibility(View.VISIBLE);
        tvTip.setText(resultCheckNfc.getAddress());
    }

    public LemonRcvAdapter<FireCheckLastDetailVo> getLemonAdapter() {
        return new CheckItemRecordAdapter(mDataList);
    }


    @Override
    public void onPullRefresh() {
        InspectionServices.init().getInspectionCheckItemRecord(resultCheckNfc.getItemId());
    }


    /**
     * 数据请求事件
     *
     * @param event
     */
    @Subscribe
    public void onEventMainThread(HttpCallbackEvent<FireCheckRecordVo> event) {
        if (EventTag.TAG_INSPECTION_CHECK_ITEM_RECORD.equals(event.getTag())) {
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                RefreshType type = (RefreshType) event.getOther();
                if (RefreshType.REFRESH == type) {
                    mPullToRefreshRecyclerView.onRefreshComplete();
                }
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    if (RefreshType.REFRESH == type) {
                        httpRefresh(event.getObj().getFireCheckList());
                    }
                    currentPage++;
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    if (RefreshType.REFRESH == type) {
                        httpRefreshError(event.getMsg());
                    }
                }
            }
        }
    }


    public void httpRefresh(List<FireCheckLastDetailVo> data) {
        if (data != null && !data.isEmpty()) {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_CONTENT);
            if (data.size() < pageSize) {
                mPullToRefreshRecyclerView.setRefreshMode(Mode.PULL_FROM_START);
            } else {
                mPullToRefreshRecyclerView.setRefreshMode(Mode.PULL_FROM_START);
            }
            mDataList.clear();
            mAdapter.notifyDataSetChanged();
            mAdapter.addItems(data);
        } else {
            mMultiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
        }
    }


    public void httpRefreshError(String msg) {
        mHUDUtil.showErrorTip(msg);
        //mMultiStateView.setEmptyTip(msg);
        mMultiStateView.setErrorTip(msg);
        mMultiStateView.setViewState(MultiStateView.VIEW_STATE_ERROR);
    }

//
//    /**
//     * 查看照片
//     *
//     * @param event
//     */
//    @Subscribe
//    public void onEventMainThread(HorizontalEvidenceEvent event) {
//        List<PhotoInfo> photos = HorizontalEvidenceAdapter.getPhotoInfoList(event.getData());
//        if (photos != null && photos.size() != 0) {
//            UIHelper.showPhotoView(getContext(), photos, event.getPosition());
//        }
//    }

}
